********************************************************************************
** 	TITLE:		mtl2017_qc2017_ces_contact_networks                           **
**  AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       CMES Merged (April 24)                                        **
**	DATE:		October 2022 					                              **	
**	VERSION:	Stata 16					                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "mtl2017_qc2017_ces_contact_networks", replace

* Open dataset

use "CMES Merged (April 24).dta", clear

* Keep 2017 Montreal and Quebec City municipal elections

keep if CITY == 2 | CITY == 3


*************************************************************
** INDEPENDENT VARIABLES: 2017 MONTREAL MUNICIPAL ELECTION **
*************************************************************

* Feeling about mayoral candidates

gen MTL17_mayorprefA = S31A_M 
gen MTL17_mayorprefB = S31B_M  
gen MTL17_mayorprefC = S31C_M  

recode MTL17_mayorprefA MTL17_mayorprefB MTL17_mayorprefC (999=.)

gen MTL17_mayorpref_complete = 1 if MTL17_mayorprefA!=. & MTL17_mayorprefB!=. & MTL17_mayorprefC!=.

gen MTL17_mayorpref = 3 if MTL17_mayorprefC >= MTL17_mayorprefA & MTL17_mayorprefC >= MTL17_mayorprefB & MTL17_mayorpref_complete == 1
replace MTL17_mayorpref = 2 if MTL17_mayorprefA == MTL17_mayorprefB & MTL17_mayorprefA == MTL17_mayorprefC & MTL17_mayorpref_complete == 1
replace MTL17_mayorpref = 1 if MTL17_mayorpref!=2 & MTL17_mayorpref!=3 & MTL17_mayorpref_complete == 1

replace MTL17_mayorpref = . if MTL17_mayorpref == 3 & MTL17_mayorprefC == MTL17_mayorprefA & MTL17_mayorpref_complete == 1
replace MTL17_mayorpref = . if MTL17_mayorpref == 3 & MTL17_mayorprefC == MTL17_mayorprefB & MTL17_mayorpref_complete == 1

label define MTL17_mayorpref 1 "Loser mayor preference" 2 "No preference" 3 "Winner mayor preference"
label values MTL17_mayorpref MTL17_mayorpref

* Mayor preference strength

gen MTL17_mayorstrengthA = MTL17_mayorprefA if MTL17_mayorprefA >= MTL17_mayorprefB & MTL17_mayorprefA >= MTL17_mayorprefC & MTL17_mayorpref_complete == 1
gen MTL17_mayorstrengthB = MTL17_mayorprefB if MTL17_mayorprefB >= MTL17_mayorprefA & MTL17_mayorprefB >= MTL17_mayorprefC & MTL17_mayorpref_complete == 1
gen MTL17_mayorstrengthC = MTL17_mayorprefC if MTL17_mayorprefC >= MTL17_mayorprefA & MTL17_mayorprefC >= MTL17_mayorprefB & MTL17_mayorpref_complete == 1

gen MTL17_mayorstrength = max(MTL17_mayorstrengthA,MTL17_mayorstrengthB,MTL17_mayorstrengthC)

replace MTL17_mayorstrength = 300 if MTL17_mayorstrength >= 80 & MTL17_mayorstrength <= 100
replace MTL17_mayorstrength = 200 if MTL17_mayorstrength >= 50 & MTL17_mayorstrength < 80
replace MTL17_mayorstrength = 100 if MTL17_mayorstrength >= 0 & MTL17_mayorstrength < 50

replace MTL17_mayorstrength = 3 if MTL17_mayorstrength == 300
replace MTL17_mayorstrength = 2 if MTL17_mayorstrength == 200
replace MTL17_mayorstrength = 1 if MTL17_mayorstrength == 100

label define MTL17_mayorstrength 1 "Weak preference" 2 "Moderate preference" 3 "Strong preference"
label values MTL17_mayorstrength MTL17_mayorstrength

* Mayor preference scale

gen MTL17_mayorscale = .

replace MTL17_mayorscale = 1 if MTL17_mayorpref == 1 & MTL17_mayorstrength == 3
replace MTL17_mayorscale = 2 if MTL17_mayorpref == 1 & MTL17_mayorstrength == 2
replace MTL17_mayorscale = 3 if MTL17_mayorpref == 1 & MTL17_mayorstrength == 1
replace MTL17_mayorscale = 4 if MTL17_mayorpref == 2
replace MTL17_mayorscale = 5 if MTL17_mayorpref == 3 & MTL17_mayorstrength == 1
replace MTL17_mayorscale = 6 if MTL17_mayorpref == 3 & MTL17_mayorstrength == 2
replace MTL17_mayorscale = 7 if MTL17_mayorpref == 3 & MTL17_mayorstrength == 3

label define MTL17_mayorscale 1 "Strong loser preference" 2 "Moderate loser preference" 3 "Weak loser preference" 4 "No preference" 5 "Weak winner preference" 6 "Moderate winner preference" 7 "Strong winner preference" 
label values MTL17_mayorscale MTL17_mayorscale

* Interest for municipal politics

gen interest = S50A // Interest for municipal politics

recode interest (99=.) // Recode "don't know" as missing data

* Education

gen education = S59 // Highest level of education completed by respondent

recode education (99=.) // Recode "don't know" as missing data

* Response date

gen sdate = CompletionDatePre // Interview date

gen surveydate = date(string(sdate,"%8.0f"),"YMD") 

format %tdMon_DD,_CCYY surveydate // Format interview date: DDMMYYYY

gen edate = 20171105 // Election date

gen electiondate = date(string(edate,"%8.0f"),"YMD") 

format %tdMon_DD,_CCYY electiondate // Format election date: DDMMYYYY

gen time = electiondate - surveydate // Number of days between interview and election

* Gender

gen male = S64 // Gender

recode male (2=0) // Recode female = 0

recode male (3=.) // Recode "other" = 0
recode male (4=.) // Recode "prefer not to say" = 0

* Age

gen yearofbirth = S2 // Year of birth

recode yearofbirth (9999=.) // Recode "don't know" as missing data

gen age = 2017 - yearofbirth // Age

replace age = . if age < 17 // Recode respondents less than 17 years old as missing data

* Name of provincial premier - Correct answer (Philippe Couillard) = 1, Incorrect answer = 0

gen premier = P22 // Provincial premier

recode premier 1=0 2=0 3=1 4=0 9=0

* Mayor prior to current mayor - Correct answer (Montreal: Gérald Tremblay, Quebec City: Andrée Boucher) = 1, Incorrect answer = 0

gen priormayor = P23 // Prior mayor

recode priormayor 2=0 3=0 4=0 9=0

* Name of the Minister of Finance of Canada - Correct answer (Bill Morneau) = 1, Incorrect answer = 0

gen financeminister = P24 // Minister of Finance of Canada

recode financeminister 1=0 2=0 3=0 4=1 9=0

* Number of City Councillors

gen councillors = P25 // Number of City Councillors (Montreal: 46, Quebec City: 21 - close answers considered as correct)

recode councillors 2=1 3=1 4=1 5=0

* Political knowledge index

gen knowledge = premier + priormayor + financeminister + councillors // Political knowledge index

* Political discussion

gen discussion_family = S54A
gen discussion_friends = S54B

recode discussion_family discussion_friends (9=.)

replace discussion_family = 4 - discussion_family
replace discussion_friends = 4 - discussion_friends

gen discussion = discussion_family + discussion_friends

gen discussion_family_3pts = discussion_family
replace discussion_family_3pts = 0 if discussion_family == 1
replace discussion_family_3pts = 1 if discussion_family == 2
replace discussion_family_3pts = 2 if discussion_family == 3

gen discussion_friends_3pts = discussion_friends
replace discussion_friends_3pts = 0 if discussion_friends == 1
replace discussion_friends_3pts = 1 if discussion_friends == 2
replace discussion_friends_3pts = 2 if discussion_friends == 3

egen discussion_3pts = rowmax(discussion_family_3pts discussion_friends_3pts)


****************************************************************
** INDEPENDENT VARIABLES: 2017 QUEBEC CITY MUNICIPAL ELECTION **
****************************************************************

* Feeling about mayoral candidates

gen QC17_mayorprefA = S31A_Q  
gen QC17_mayorprefB = S31B_Q  
gen QC17_mayorprefC = S31C_Q  
gen QC17_mayorprefD = S31D_Q  
gen QC17_mayorprefE = S31E_Q  
gen QC17_mayorprefF = S31F_Q  

recode QC17_mayorprefA QC17_mayorprefB QC17_mayorprefC QC17_mayorprefD QC17_mayorprefE QC17_mayorprefF (999=.)

gen QC17_mayorpref_complete = 1 if QC17_mayorprefA!=. & QC17_mayorprefB!=. & QC17_mayorprefC!=. & QC17_mayorprefD!=. & QC17_mayorprefE!=. & QC17_mayorprefF!=.

gen QC17_mayorpref = 3 if QC17_mayorprefE >= QC17_mayorprefA & QC17_mayorprefE >= QC17_mayorprefB & QC17_mayorprefE >= QC17_mayorprefC & QC17_mayorprefE >= QC17_mayorprefD & QC17_mayorprefE >= QC17_mayorprefF & QC17_mayorpref_complete == 1
replace QC17_mayorpref = 2 if QC17_mayorprefA == QC17_mayorprefB & QC17_mayorprefA == QC17_mayorprefC & QC17_mayorprefA == QC17_mayorprefD & QC17_mayorprefA == QC17_mayorprefE & QC17_mayorprefA == QC17_mayorprefF & QC17_mayorpref_complete == 1
replace QC17_mayorpref = 1 if QC17_mayorpref!=2 & QC17_mayorpref!=3 & QC17_mayorpref_complete == 1

replace QC17_mayorpref = . if QC17_mayorpref == 3 & QC17_mayorprefE == QC17_mayorprefA & QC17_mayorpref_complete == 1
replace QC17_mayorpref = . if QC17_mayorpref == 3 & QC17_mayorprefE == QC17_mayorprefB & QC17_mayorpref_complete == 1
replace QC17_mayorpref = . if QC17_mayorpref == 3 & QC17_mayorprefE == QC17_mayorprefC & QC17_mayorpref_complete == 1
replace QC17_mayorpref = . if QC17_mayorpref == 3 & QC17_mayorprefE == QC17_mayorprefD & QC17_mayorpref_complete == 1
replace QC17_mayorpref = . if QC17_mayorpref == 3 & QC17_mayorprefE == QC17_mayorprefF & QC17_mayorpref_complete == 1

label define QC17_mayorpref 1 "Loser Mayor" 2 "No preference" 3 "Winner Mayor"
label values QC17_mayorpref QC17_mayorpref

* Mayor preference strength

gen QC17_mayorstrengthA = QC17_mayorprefA if QC17_mayorprefA >= QC17_mayorprefB & QC17_mayorprefA >= QC17_mayorprefC & QC17_mayorprefA >= QC17_mayorprefD & QC17_mayorprefA >= QC17_mayorprefE & QC17_mayorprefA >= QC17_mayorprefF & QC17_mayorpref_complete == 1
gen QC17_mayorstrengthB = QC17_mayorprefB if QC17_mayorprefB >= QC17_mayorprefA & QC17_mayorprefB >= QC17_mayorprefC & QC17_mayorprefB >= QC17_mayorprefD & QC17_mayorprefB >= QC17_mayorprefE & QC17_mayorprefB >= QC17_mayorprefF & QC17_mayorpref_complete == 1
gen QC17_mayorstrengthC = QC17_mayorprefC if QC17_mayorprefC >= QC17_mayorprefA & QC17_mayorprefC >= QC17_mayorprefB & QC17_mayorprefC >= QC17_mayorprefD & QC17_mayorprefC >= QC17_mayorprefE & QC17_mayorprefC >= QC17_mayorprefF & QC17_mayorpref_complete == 1
gen QC17_mayorstrengthD = QC17_mayorprefD if QC17_mayorprefD >= QC17_mayorprefA & QC17_mayorprefD >= QC17_mayorprefB & QC17_mayorprefD >= QC17_mayorprefC & QC17_mayorprefD >= QC17_mayorprefE & QC17_mayorprefD >= QC17_mayorprefF & QC17_mayorpref_complete == 1
gen QC17_mayorstrengthE = QC17_mayorprefE if QC17_mayorprefE >= QC17_mayorprefA & QC17_mayorprefE >= QC17_mayorprefB & QC17_mayorprefE >= QC17_mayorprefC & QC17_mayorprefE >= QC17_mayorprefD & QC17_mayorprefE >= QC17_mayorprefF & QC17_mayorpref_complete == 1
gen QC17_mayorstrengthF = QC17_mayorprefF if QC17_mayorprefF >= QC17_mayorprefA & QC17_mayorprefF >= QC17_mayorprefB & QC17_mayorprefF >= QC17_mayorprefC & QC17_mayorprefF >= QC17_mayorprefD & QC17_mayorprefF >= QC17_mayorprefE & QC17_mayorpref_complete == 1

gen QC17_mayorstrength = max(QC17_mayorstrengthA,QC17_mayorstrengthB,QC17_mayorstrengthC,QC17_mayorstrengthD,QC17_mayorstrengthE,QC17_mayorstrengthF)

replace QC17_mayorstrength = 300 if QC17_mayorstrength >= 80 & QC17_mayorstrength <= 100
replace QC17_mayorstrength = 200 if QC17_mayorstrength >= 50 & QC17_mayorstrength < 80
replace QC17_mayorstrength = 100 if QC17_mayorstrength >= 0 & QC17_mayorstrength < 50

replace QC17_mayorstrength = 3 if QC17_mayorstrength == 300
replace QC17_mayorstrength = 2 if QC17_mayorstrength == 200
replace QC17_mayorstrength = 1 if QC17_mayorstrength == 100

label define QC17_mayorstrength 1 "Weak preference" 2 "Moderate preference" 3 "Strong preference"
label values QC17_mayorstrength QC17_mayorstrength

* Mayor preference scale

gen QC17_mayorscale = .

replace QC17_mayorscale = 1 if QC17_mayorpref == 1 & QC17_mayorstrength == 3
replace QC17_mayorscale = 2 if QC17_mayorpref == 1 & QC17_mayorstrength == 2
replace QC17_mayorscale = 3 if QC17_mayorpref == 1 & QC17_mayorstrength == 1
replace QC17_mayorscale = 4 if QC17_mayorpref == 2
replace QC17_mayorscale = 5 if QC17_mayorpref == 3 & QC17_mayorstrength == 1
replace QC17_mayorscale = 6 if QC17_mayorpref == 3 & QC17_mayorstrength == 2
replace QC17_mayorscale = 7 if QC17_mayorpref == 3 & QC17_mayorstrength == 3

label define QC17_mayorscale 1 "Strong loser preference" 2 "Moderate loser preference" 3 "Weak loser preference" 4 "No preference" 5 "Weak winner preference" 6 "Moderate winner preference" 7 "Strong winner preference" 
label values QC17_mayorscale QC17_mayorscale


****************************************************
** EXPECTATIONS: 2017 MONTREAL MUNICIPAL ELECTION **
****************************************************

* Generate variable identifying incomplete answers

gen MTL17_coderre_chance_whole = S34A_M
gen MTL17_fortier_chance_whole = S34B_M
gen MTL17_plante_chance_whole = S34C_M

recode MTL17_coderre_chance_whole MTL17_fortier_chance_whole MTL17_plante_chance_whole (999=0)

gen MTL17_complete_whole = 1 if MTL17_coderre_chance_whole!=. & MTL17_fortier_chance_whole!=. & MTL17_plante_chance_whole!=.

* Generate value of 1 if the expectation for one party is higher or equal to the expectations for at least one other party

gen MTL17_coderre_whole = 1 if MTL17_coderre_chance_whole >= MTL17_fortier_chance_whole & MTL17_coderre_chance_whole >= MTL17_plante_chance_whole & MTL17_complete_whole == 1
gen MTL17_fortier_whole = 1 if MTL17_fortier_chance_whole >= MTL17_coderre_chance_whole & MTL17_fortier_chance_whole >= MTL17_plante_chance_whole & MTL17_complete_whole == 1
gen MTL17_plante_whole = 1 if MTL17_plante_chance_whole >= MTL17_coderre_chance_whole & MTL17_plante_chance_whole >= MTL17_fortier_chance_whole & MTL17_complete_whole == 1
gen MTL17_oth_whole = 1 if MTL17_coderre_chance_whole == 0 & MTL17_fortier_chance_whole == 0 & MTL17_plante_chance_whole == 0

replace MTL17_coderre_whole = . if MTL17_oth_whole == 1 & MTL17_complete_whole == 1
replace MTL17_fortier_whole = . if MTL17_oth_whole == 1 & MTL17_complete_whole == 1
replace MTL17_plante_whole = . if MTL17_oth_whole == 1 & MTL17_complete_whole == 1

* Generate variable for ambiguous forecasts

gen MTL17_ambiguous_whole = .

replace MTL17_ambiguous_whole = 99 if MTL17_coderre_whole == MTL17_fortier_whole & MTL17_coderre_whole!=. & MTL17_complete_whole == 1 | MTL17_coderre_whole == MTL17_plante_whole & MTL17_coderre_whole!=. & MTL17_complete_whole == 1
replace MTL17_ambiguous_whole = 99 if MTL17_fortier_whole == MTL17_coderre_whole & MTL17_fortier_whole!=. & MTL17_complete_whole == 1 | MTL17_fortier_whole == MTL17_plante_whole & MTL17_fortier_whole!=. & MTL17_complete_whole == 1
replace MTL17_ambiguous_whole = 99 if MTL17_plante_whole == MTL17_coderre_whole & MTL17_plante_whole!=. & MTL17_complete_whole == 1 | MTL17_plante_whole == MTL17_fortier_whole & MTL17_plante_whole!=. & MTL17_complete_whole == 1

* Generate value of 0 for lower expectations

replace MTL17_coderre_whole = 0 if MTL17_coderre_whole!=1 & MTL17_complete_whole == 1
replace MTL17_fortier_whole = 0 if MTL17_fortier_whole!=1 & MTL17_complete_whole == 1
replace MTL17_plante_whole = 0 if MTL17_plante_whole!=1 & MTL17_complete_whole == 1
replace MTL17_oth_whole = 0 if MTL17_oth_whole!=1 & MTL17_complete_whole == 1

* Forecasts (1 = Coderre, 2 = Fortier, 3 = Plante, 88 = other, 99 = ambiguous)

gen MTL17_forecast_whole = .

replace MTL17_forecast_whole = 1 if MTL17_coderre_whole == 1 & MTL17_complete_whole == 1
replace MTL17_forecast_whole = 2 if MTL17_fortier_whole == 1 & MTL17_complete_whole == 1
replace MTL17_forecast_whole = 3 if MTL17_plante_whole == 1 & MTL17_complete_whole == 1
replace MTL17_forecast_whole = 88 if MTL17_oth_whole == 1 & MTL17_complete_whole == 1
replace MTL17_forecast_whole = 99 if MTL17_ambiguous_whole == 99 & MTL17_complete_whole == 1

* Identify correct and ambiguous forecasts

gen MTL17_correct_whole = 0 if MTL17_forecast_whole!=3 & MTL17_forecast_whole!=99 & MTL17_complete_whole == 1
replace MTL17_correct_whole = 1 if MTL17_forecast_whole == 3 & MTL17_forecast_whole!=99 & MTL17_complete_whole == 1

replace MTL17_correct_whole = 0 if MTL17_forecast_whole == 99 & MTL17_plante_whole!=1 & MTL17_complete_whole == 1

replace MTL17_correct_whole = 0 if S34A_M == 999 & S34B_M == 999 & S34C_M == 999

replace MTL17_correct_whole = 99 if MTL17_forecast_whole == 99 & MTL17_plante_whole == 1 & MTL17_complete_whole == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen MTL17_correct_whole_d = MTL17_correct_whole

replace MTL17_correct_whole_d = 0 if MTL17_correct_whole_d == 99
replace MTL17_correct_whole_d = 1 if MTL17_correct_whole_d == 1


**************************************************
** EXPECTATIONS: 2017 QUEBEC MUNICIPAL ELECTION **
**************************************************

* Generate variable identifying incomplete answers

gen QC17_brisson_chance_whole = S34A_Q
gen QC17_gagnon_chance_whole = S34B_Q
gen QC17_gosselin_chance_whole = S34C_Q
gen QC17_guerette_chance_whole = S34D_Q
gen QC17_labeaume_chance_whole = S34E_Q
gen QC17_lavigne_chance_whole = S34F_Q

recode QC17_brisson_chance_whole QC17_gagnon_chance_whole QC17_gosselin_chance_whole QC17_guerette_chance_whole QC17_labeaume_chance_whole QC17_lavigne_chance_whole (999=.)

gen QC17_complete_whole = 1 if QC17_brisson_chance_whole!=. & QC17_gagnon_chance_whole!=. & QC17_gosselin_chance_whole!=. & QC17_guerette_chance_whole!=. & QC17_labeaume_chance_whole!=. & QC17_lavigne_chance_whole!=.

* Generate value of 1 if the expectation for one party is higher or equal to the expectations for at least one other party

gen QC17_brisson_whole = 1 if QC17_brisson_chance_whole >= QC17_gagnon_chance_whole & QC17_brisson_chance_whole >= QC17_gosselin_chance_whole & QC17_brisson_chance_whole >= QC17_guerette_chance_whole & QC17_brisson_chance_whole >= QC17_labeaume_chance_whole & QC17_brisson_chance_whole >= QC17_lavigne_chance_whole & QC17_complete_whole == 1
gen QC17_gagnon_whole = 1 if QC17_gagnon_chance_whole >= QC17_brisson_chance_whole & QC17_gagnon_chance_whole >= QC17_gosselin_chance_whole & QC17_gagnon_chance_whole >= QC17_guerette_chance_whole & QC17_gagnon_chance_whole >= QC17_labeaume_chance_whole & QC17_gagnon_chance_whole >= QC17_lavigne_chance_whole & QC17_complete_whole == 1
gen QC17_gosselin_whole = 1 if QC17_gosselin_chance_whole >= QC17_brisson_chance_whole & QC17_gosselin_chance_whole >= QC17_gagnon_chance_whole & QC17_gosselin_chance_whole >= QC17_guerette_chance_whole & QC17_gosselin_chance_whole >= QC17_labeaume_chance_whole & QC17_gosselin_chance_whole >= QC17_lavigne_chance_whole & QC17_complete_whole == 1
gen QC17_guerette_whole = 1 if QC17_guerette_chance_whole >= QC17_brisson_chance_whole & QC17_guerette_chance_whole >= QC17_gagnon_chance_whole & QC17_guerette_chance_whole >= QC17_gosselin_chance_whole & QC17_guerette_chance_whole >= QC17_labeaume_chance_whole & QC17_guerette_chance_whole >= QC17_lavigne_chance_whole & QC17_complete_whole == 1
gen QC17_labeaume_whole = 1 if QC17_labeaume_chance_whole >= QC17_brisson_chance_whole & QC17_labeaume_chance_whole >= QC17_gagnon_chance_whole & QC17_labeaume_chance_whole >= QC17_gosselin_chance_whole & QC17_labeaume_chance_whole >= QC17_guerette_chance_whole & QC17_labeaume_chance_whole >= QC17_lavigne_chance_whole & QC17_complete_whole == 1
gen QC17_lavigne_whole = 1 if QC17_lavigne_chance_whole >= QC17_brisson_chance_whole & QC17_lavigne_chance_whole >= QC17_gagnon_chance_whole & QC17_lavigne_chance_whole >= QC17_gosselin_chance_whole & QC17_lavigne_chance_whole >= QC17_guerette_chance_whole & QC17_lavigne_chance_whole >= QC17_labeaume_chance_whole & QC17_complete_whole == 1

gen QC17_oth_whole = 1 if QC17_brisson_chance_whole == 0 & QC17_gagnon_chance_whole == 0 & QC17_gosselin_chance_whole == 0 & QC17_guerette_chance_whole == 0 & QC17_labeaume_chance_whole == 0 & QC17_lavigne_chance_whole == 0

replace QC17_brisson_whole = . if QC17_oth_whole == 1 & QC17_complete_whole == 1
replace QC17_gagnon_whole = . if QC17_oth_whole == 1 & QC17_complete_whole == 1
replace QC17_gosselin_whole = . if QC17_oth_whole == 1 & QC17_complete_whole == 1
replace QC17_guerette_whole = . if QC17_oth_whole == 1 & QC17_complete_whole == 1
replace QC17_labeaume_whole = . if QC17_oth_whole == 1 & QC17_complete_whole == 1
replace QC17_lavigne_whole = . if QC17_oth_whole == 1 & QC17_complete_whole == 1

* Generate variable for ambiguous forecasts

gen QC17_ambiguous_whole = .

replace QC17_ambiguous_whole = 99 if QC17_brisson_whole == QC17_gagnon_whole & QC17_brisson_whole!=. & QC17_complete_whole == 1 | QC17_brisson_whole == QC17_gosselin_whole & QC17_brisson_whole!=. & QC17_complete_whole == 1 | QC17_brisson_whole == QC17_guerette_whole & QC17_brisson_whole!=. & QC17_complete_whole == 1 | QC17_brisson_whole == QC17_labeaume_whole & QC17_brisson_whole!=. & QC17_complete_whole == 1 | QC17_brisson_whole == QC17_lavigne_whole & QC17_brisson_whole!=. & QC17_complete_whole == 1

replace QC17_ambiguous_whole = 99 if QC17_gagnon_whole == QC17_brisson_whole & QC17_gagnon_whole!=. & QC17_complete_whole == 1 | QC17_gagnon_whole == QC17_gosselin_whole & QC17_gagnon_whole!=. & QC17_complete_whole == 1 | QC17_gagnon_whole == QC17_guerette_whole & QC17_gagnon_whole!=. & QC17_complete_whole == 1 | QC17_gagnon_whole == QC17_labeaume_whole & QC17_gagnon_whole!=. & QC17_complete_whole == 1 | QC17_gagnon_whole == QC17_lavigne_whole & QC17_gagnon_whole!=. & QC17_complete_whole == 1

replace QC17_ambiguous_whole = 99 if QC17_gosselin_whole == QC17_brisson_whole & QC17_gosselin_whole!=. & QC17_complete_whole == 1 | QC17_gosselin_whole == QC17_gagnon_whole & QC17_gosselin_whole!=. & QC17_complete_whole == 1 | QC17_gosselin_whole == QC17_guerette_whole & QC17_gosselin_whole!=. & QC17_complete_whole == 1 | QC17_gosselin_whole == QC17_labeaume_whole & QC17_gosselin_whole!=. & QC17_complete_whole == 1 | QC17_gosselin_whole == QC17_lavigne_whole & QC17_gosselin_whole!=. & QC17_complete_whole == 1

replace QC17_ambiguous_whole = 99 if QC17_guerette_whole == QC17_brisson_whole & QC17_guerette_whole!=. & QC17_complete_whole == 1 | QC17_guerette_whole == QC17_gagnon_whole & QC17_guerette_whole!=. & QC17_complete_whole == 1 | QC17_guerette_whole == QC17_gosselin_whole & QC17_guerette_whole!=. & QC17_complete_whole == 1 | QC17_guerette_whole == QC17_labeaume_whole & QC17_guerette_whole!=. & QC17_complete_whole == 1 | QC17_guerette_whole == QC17_lavigne_whole & QC17_guerette_whole!=. & QC17_complete_whole == 1

replace QC17_ambiguous_whole = 99 if QC17_labeaume_whole == QC17_brisson_whole & QC17_labeaume_whole!=. & QC17_complete_whole == 1 | QC17_labeaume_whole == QC17_gagnon_whole & QC17_labeaume_whole!=. & QC17_complete_whole == 1 | QC17_labeaume_whole == QC17_gosselin_whole & QC17_labeaume_whole!=. & QC17_complete_whole == 1 | QC17_labeaume_whole == QC17_guerette_whole & QC17_labeaume_whole!=. & QC17_complete_whole == 1 | QC17_labeaume_whole == QC17_lavigne_whole & QC17_labeaume_whole!=. & QC17_complete_whole == 1

replace QC17_ambiguous_whole = 99 if QC17_lavigne_whole == QC17_brisson_whole & QC17_lavigne_whole!=. & QC17_complete_whole == 1 | QC17_lavigne_whole == QC17_gagnon_whole & QC17_lavigne_whole!=. & QC17_complete_whole == 1 | QC17_lavigne_whole == QC17_gosselin_whole & QC17_lavigne_whole!=. & QC17_complete_whole == 1 | QC17_lavigne_whole == QC17_guerette_whole & QC17_lavigne_whole!=. & QC17_complete_whole == 1 | QC17_lavigne_whole == QC17_labeaume_whole & QC17_lavigne_whole!=. & QC17_complete_whole == 1

* Generate value of 0 for lower expectations

replace QC17_brisson_whole = 0 if QC17_brisson_whole!=1 & QC17_complete_whole == 1
replace QC17_gagnon_whole = 0 if QC17_gagnon_whole!=1 & QC17_complete_whole == 1
replace QC17_gosselin_whole = 0 if QC17_gosselin_whole!=1 & QC17_complete_whole == 1
replace QC17_guerette_whole = 0 if QC17_guerette_whole!=1 & QC17_complete_whole == 1
replace QC17_labeaume_whole = 0 if QC17_labeaume_whole!=1 & QC17_complete_whole == 1
replace QC17_lavigne_whole = 0 if QC17_lavigne_whole!=1 & QC17_complete_whole == 1
replace QC17_oth_whole = 0 if QC17_oth_whole!=1 & QC17_complete_whole == 1

* Forecasts (1 = Brisson, 2 = Gagnon, 3 = Gosselin, 4 = Guerette, 5 = Labeaume, 6 = Lavigne, 88 = other, 99 = ambiguous)

gen QC17_forecast_whole = .

replace QC17_forecast_whole = 1 if QC17_brisson_whole == 1 & QC17_complete_whole == 1
replace QC17_forecast_whole = 2 if QC17_gagnon_whole == 1 & QC17_complete_whole == 1
replace QC17_forecast_whole = 3 if QC17_gosselin_whole == 1 & QC17_complete_whole == 1
replace QC17_forecast_whole = 4 if QC17_guerette_whole == 1 & QC17_complete_whole == 1
replace QC17_forecast_whole = 5 if QC17_labeaume_whole == 1 & QC17_complete_whole == 1
replace QC17_forecast_whole = 6 if QC17_lavigne_whole == 1 & QC17_complete_whole == 1
replace QC17_forecast_whole = 88 if QC17_oth_whole == 1 & QC17_complete_whole == 1
replace QC17_forecast_whole = 99 if QC17_ambiguous_whole == 99 & QC17_complete_whole == 1

* Identify correct and ambiguous forecasts

gen QC17_correct_whole = 0 if QC17_forecast_whole!=5 & QC17_forecast_whole!=99 & QC17_complete_whole == 1
replace QC17_correct_whole = 1 if QC17_forecast_whole == 5 & QC17_forecast_whole!=99 & QC17_complete_whole == 1

replace QC17_correct_whole = 0 if QC17_forecast_whole == 99 & QC17_labeaume_whole!=1 & QC17_complete_whole == 1

replace QC17_correct_whole = 0 if S34A_Q == 999 & S34B_Q == 999 & S34C_Q == 999 & S34D_Q == 999 & S34E_Q == 999 & S34F_Q == 999

replace QC17_correct_whole = 99 if QC17_forecast_whole == 99 & QC17_labeaume_whole == 1 & QC17_complete_whole == 1

* Generate dichotomous variable (treat ambiguous forecasts as incorrect)

gen QC17_correct_whole_d = QC17_correct_whole

replace QC17_correct_whole_d = 0 if QC17_correct_whole_d == 99
replace QC17_correct_whole_d = 1 if QC17_correct_whole_d == 1


**********
** SAVE **
**********

save "mtl2017_qc2017_cmes.dta", replace


*****************************************************
** MAIN ANALYSES: 2017 MONTREAL MUNICIPAL ELECTION **
*****************************************************

* Regression analysis and percentage of correct and incorrect forecasts

logistic MTL17_correct_whole_d discussion MTL17_mayorprefC interest knowledge i.male age education time if CITY == 2 & time > 0
estimates table, star(.05 .01 .001)

tab MTL17_correct_whole_d if e(sample) == 1


**********************************************************
** SUMMARY STATISTICS: 2017 MONTREAL MUNICIPAL ELECTION **
**********************************************************

label variable MTL17_correct_whole_d "Mayor (DV)"
label variable MTL17_mayorprefC "Plante rating"
label variable discussion "Discussion"
label variable interest "Interest"
label variable knowledge "Political knowledge"
label variable male "Gender (male = 1)"
label variable age "Age"
label variable education "Education"
label variable time "Time of interview"

quietly logistic MTL17_correct_whole_d discussion MTL17_mayorprefC interest knowledge i.male age education time if CITY == 2

estpost tabstat MTL17_correct_whole_d discussion MTL17_mayorprefC interest knowledge male age education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "mtl2017_cmes_summary_municipal.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2017 Montreal municipal election (CMES), municipal level") replace

eststo clear


********************************************************
** MAIN ANALYSES: 2017 QUEBEC CITY MUNICIPAL ELECTION **
********************************************************

* Regression analysis and percentage of correct and incorrect forecasts

logistic QC17_correct_whole_d discussion QC17_mayorprefE interest knowledge i.male age education time if CITY == 3 & time > 0
estimates table, star(.05 .01 .001)

tab QC17_correct_whole_d if e(sample) == 1


*************************************************************
** SUMMARY STATISTICS: 2017 QUEBEC CITY MUNICIPAL ELECTION **
*************************************************************

label variable QC17_correct_whole_d "Mayor (DV)"
label variable QC17_mayorprefE "Labeaume rating"
label variable discussion "Discussion"
label variable interest "Interest"
label variable knowledge "Political knowledge"
label variable male "Gender (male = 1)"
label variable age "Age"
label variable education "Education"
label variable time "Time of interview"

quietly logistic QC17_correct_whole_d discussion QC17_mayorprefE interest knowledge i.male age education time if CITY == 3

estpost tabstat QC17_correct_whole_d discussion QC17_mayorprefE interest knowledge male age education time if e(sample) == 1, statistics(n mean p50 sd min max) columns(statistics)

esttab using "qc2017_cmes_summary_municipal.tex", substitute("\begin{table}[htbp]" "\begin{table}[H]") cells("count(label(N) fmt(%9.0fc)) mean(fmt(%5.1f) label(Mean)) p50(fmt(%5.1f) label(Median)) sd(fmt(%5.1f) label(Std. dev.)) min(fmt(%5.1f) label(Min)) max(fmt(%5.1f) label(Max))") label width(\textwidth) nomtitle nonumber noobs booktabs title("Summary statistics -- 2017 Quebec City municipal election (CMES), municipal level") replace

eststo clear

log close